package com.example.lotapp.utils.json;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.text.TextUtils;

public class JsonType {
	public static boolean integerSimilar(Object object) {
		return object instanceof Byte
					|| object instanceof Short
					|| object instanceof Integer
					|| object instanceof Long;
	}
	
	public static Boolean toBoolean(Object object) {
		if (object instanceof Boolean) {
			return (Boolean)object;
		}
		if (object instanceof String) {
			if ("true".equalsIgnoreCase((String)object)) {
				return true;
			}
			if ("false".equalsIgnoreCase((String)object)) {
				return false;
			}
		}
		Long tmp = toLong(object);
		if (tmp != null) {
			return tmp != 0 ? true : false;
		}
		return null;
	}
	
	public static Byte toByte(Object object) {
		Long tmp = toLong(object);
		if (tmp != null) {
			long v = tmp;
			if (v <= Byte.MAX_VALUE  && v >= Byte.MIN_VALUE) {
				return Byte.valueOf((byte)v);
			}
		}
		return null;
	}
	
	public static Short toShort(Object object) {
		Long tmp = toLong(object);
		if (tmp != null) {
			long v = tmp;
			if (v <= Short.MAX_VALUE  && v >= Short.MIN_VALUE) {
				return Short.valueOf((short)v);
			}
		}
		return null;
	}
	
	public static Integer toInteger(Object object) {
		Long tmp = toLong(object);
		if (tmp != null) {
			long v = tmp;
			if (v <= Integer.MAX_VALUE  && v >= Integer.MIN_VALUE) {
				return Integer.valueOf((int)v);
			}
		}
		return null;
	}
	
	public static Long toLong(Object object) {
		if (integerSimilar(object) || object instanceof String) {
			try {
				return Long.valueOf(object.toString());
			} catch (NumberFormatException e) {
			}
		}
		return null;
	}
	
	public static Float toFloat(Object object) {
		if (object instanceof Float
				|| object instanceof Double
				|| object instanceof String) {
			try {
				return Float.valueOf(object.toString());
			} catch (NumberFormatException e) {
			}
		}
		return null;
	}
	
	public static Double toDouble(Object object) {
		if (object instanceof Float 
				|| object instanceof Double
				|| object instanceof String) {
			try {
				return Double.valueOf(object.toString());
			} catch (NumberFormatException e) {
			}
		}
		return null;
	}
	
	public static BigInteger toBigInteger(Object object) {
		if (object instanceof String) {
			return new BigInteger((String)object); 
		}
		
		if (integerSimilar(object)) {
			return BigInteger.valueOf(Long.valueOf(object.toString()));
		}
		
		if (object instanceof Boolean) {
			return BigInteger.valueOf(((Boolean)object) == Boolean.TRUE ? 1 : 0);
		}
		
		return null;
	}
	
	public static BigDecimal toBigDecimal(Object object) {
		if (object instanceof String) {
			return new BigDecimal((String)object); 
		}
		
		if (object instanceof Double) {
			return BigDecimal.valueOf((Double)object);
		}
		
		if (object instanceof Float) {
			return BigDecimal.valueOf((Float)object);
		}
		
		BigInteger bigInteger = toBigInteger(object);
		if (bigInteger != null) {
			return new BigDecimal(bigInteger);
		}
		return null;
	}
	
	public static Date toDate(Object object, String format) {
		if (integerSimilar(object) || object instanceof String) {
			try {
				long millis = Long.valueOf(object.toString());
				return new Date(millis);
			} catch (NumberFormatException e) {
			}
		} 
		if (!TextUtils.isEmpty(format) && (object instanceof String)) {
			try {
				SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		        return dateFormat.parse((String)object);
			} catch (java.text.ParseException e) {
			} catch (IllegalArgumentException e) {
			} 
		}
		return null;
	}
}
